<?php
//自动生成,请根据需要修改
namespace app\rechargecard\controller;
use app\common\controller\Base;
use think\Config;
use think\facade\Request;
use think\Db;
/**
* @title 充值卡接口
* @description 充值卡说明
* @group 充值卡
*/
class RechargeCard extends Base{

    /**
     * @title 插入记录
     * @description 插入充值卡数据
     * @author 开发者（范浩婷）
     * @url /rechargecard/recharge_card/add
     * @method post
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:account_id type:VarChar require:1 default:0 other: desc:账户
     * @param name:recharge_card type:VarChar require:1 default:0 other: desc:充值卡
     * @param name:recharge_time type:Int require:1 default:0 other: desc:充值时间
     * @param name:amount type:Decimal require:1 default:0 other: desc:金额
     * @param name:status type:TinyInt require:1 default:0 other: desc:状态
     *
     *
     * @return account_id:账户
     * @return recharge_card:充值卡
     * @return recharge_time:充值时间
     * @return amount:金额
     * @return status:状态
     *
     */
    public function add (){
        //模块 - 输入数据
        $input['account_id'] = Request::post('account_id');
        $input['recharge_card'] = Request::post('recharge_card');
        $input['recharge_time'] = Request::post('recharge_time/d');
        $input['amount'] = Request::post('amount');
        $input['status'] = Request::post('status');


        //模块 - 验证器
        $recharge_cardValidate = new \app\rechargecard\validate\RechargeCard;
        $validate = $recharge_cardValidate->scene('except_id')->check($input);
        if (!$validate) {
            $msg['msg'] = '验证失败';
            $msg['validate'] = $recharge_cardValidate->getError();
            return $this->errorJson($msg);
        }

        // ---	模块 - 新增  ---
        //插入的数据
        $insert_data = [
            'account_id'=>$input['account_id'],
            'recharge_card'=>$input['recharge_card'],
            'recharge_time'=>$input['recharge_time'],
            'amount'=>$input['amount'],
            'status'=>$input['status'],
        ];
        //添加数据
        $insert_res = Db::name('recharge_card')->data($insert_data)->insert();

        if(!$insert_res){
            $msg['msg'] = '插入数据失败！';
            return $this->errorJson($msg);
        }
        $msg['res'] = $insert_res;


        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
    /**
     * @title 获取列表数据（paginate）
     * @description 获取充值卡的列表数据（分页）
     * @author 开发者（范浩婷）
     * @url /rechargecard/recharge_card/get_list
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:Int require:1 default:0 other: desc:id
     * @param name:account_id type:VarChar require:1 default:0 other: desc:账户
     * @param name:recharge_card type:VarChar require:1 default:0 other: desc:充值卡
     * @param name:recharge_time type:Int require:1 default:0 other: desc:充值时间
     * @param name:amount type:Decimal require:1 default:0 other: desc:金额
     * @param name:status type:TinyInt require:1 default:0 other: desc:状态
     *
     * @param name:page type:Int require:0 default:0 other: desc:当前页
     * @param name:pagesize type:Int require:0 default:0 other: desc:每页数量
     *
     * @return id:id
     * @return account_id:账户
     * @return recharge_card:充值卡
     * @return recharge_time:充值时间
     * @return amount:金额
     * @return status:状态
     *
     */
    public function get_list (){
        //模块 - 输入数据
        $input['id'] = Request::param('id/d');
        $input['account_id'] = Request::param('account_id');
        $input['recharge_card'] = Request::param('recharge_card');
        $input['recharge_time'] = Request::param('recharge_time/d');
        $input['amount'] = Request::param('amount');
        $input['status'] = Request::param('status');

        // ---	模块 - 查询  ---
        //查询条件
        $temp_map = [
            ['id','=',$input['id']],
            ['account_id','like','%'.$input['account_id'].'%'],
            ['recharge_card','like','%'.$input['recharge_card'].'%'],
            ['recharge_time','=',$input['recharge_time']],
            ['amount','=',$input['amount']],
            ['status','=',$input['status']],
        ];
        //去除为空的条件
        $paginate_map = [];
        foreach ($temp_map as $k=>$v){
            if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
                continue;
            }
            array_push($paginate_map,$v);
        }

        //获取数据
        $select_res = Db::name('recharge_card')->where($paginate_map)->paginate();

        // paginate 返回数据
        $msg['list'] = $select_res;


        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
    /**
     * @title 获取列表数据（select）
     * @description 获取充值卡的列表数据（无分页）
     * @author 开发者（范浩婷）
     * @url /rechargecard/recharge_card/get_select
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:Int require:1 default:0 other: desc:id
     * @param name:account_id type:VarChar require:1 default:0 other: desc:账户
     * @param name:recharge_card type:VarChar require:1 default:0 other: desc:充值卡
     * @param name:recharge_time type:Int require:1 default:0 other: desc:充值时间
     * @param name:amount type:Decimal require:1 default:0 other: desc:金额
     * @param name:status type:TinyInt require:1 default:0 other: desc:状态
     *
     *
     * @return id:id
     * @return account_id:账户
     * @return recharge_card:充值卡
     * @return recharge_time:充值时间
     * @return amount:金额
     * @return status:状态
     *
     */
    public function get_select (){
        //模块 - 输入数据
        $input['id'] = Request::param('id/d');
        $input['account_id'] = Request::param('account_id');
        $input['recharge_card'] = Request::param('recharge_card');
        $input['recharge_time'] = Request::param('recharge_time/d');
        $input['amount'] = Request::param('amount');
        $input['status'] = Request::param('status');

        // ---	模块 - 查询  ---
        //查询条件
        $temp_map = [
            ['id','=',$input['id']],
            ['account_id','like','%'.$input['account_id'].'%'],
            ['recharge_card','like','%'.$input['recharge_card'].'%'],
            ['recharge_time','=',$input['recharge_time']],
            ['amount','=',$input['amount']],
            ['status','=',$input['status']],
        ];
        //去除为空的条件
        $select_map = [];
        foreach ($temp_map as $k=>$v){
            if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
                continue;
            }
            array_push($select_map,$v);
        }

        //获取数据
        $select_res = Db::name('recharge_card')->where($select_map)->select();

        // select 返回数据
        $msg['list'] = $select_res;


        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
	/**
	* @title 获取单条数据
	* @description 根据ID获取充值卡的指定记录
	* @author 开发者（范浩婷）
	* @url /rechargecard/recharge_card/info
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	*
	*
	* @return id:id
	*
	*/
	public function info (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');

		// input结果返回
		$msg['data']['id'] = $input['id'];

		//模块 - 验证器
		$recharge_cardValidate = new \app\rechargecard\validate\RechargeCard;
		$validate = $recharge_cardValidate->scene('only_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $recharge_cardValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
		];
		//去除为空的条件
		$find_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($find_map,$v);
		}

		//获取数据
		$select_res = Db::name('recharge_card')->where($find_map)->find();

		// find 返回数据
		$msg['data'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 更新记录
	* @description 根据ID更新充值卡指定记录的数据
	* @author 开发者（范浩婷）
	* @url /rechargecard/recharge_card/edit
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:account_id type:VarChar require:1 default:0 other: desc:账户
	* @param name:recharge_card type:VarChar require:1 default:0 other: desc:充值卡
	* @param name:recharge_time type:Int require:1 default:0 other: desc:充值时间
	* @param name:amount type:Decimal require:1 default:0 other: desc:金额
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态
	*
	*
	* @return id:id
	* @return account_id:账户
	* @return recharge_card:充值卡
	* @return recharge_time:充值时间
	* @return amount:金额
	* @return status:状态
	*
	*/
	public function edit (){
		//模块 - 输入数据
		$input['id'] = Request::post('id/d');
		$input['account_id'] = Request::post('account_id');
		$input['recharge_card'] = Request::post('recharge_card');
		$input['recharge_time'] = Request::post('recharge_time/d');
		$input['amount'] = Request::post('amount');
		$input['status'] = Request::post('status');

		//模块 - 验证器
		$recharge_cardValidate = new \app\rechargecard\validate\RechargeCard;
		$validate = $recharge_cardValidate->scene('all_field')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $recharge_cardValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 修改  ---
		//更新的条件
		$update_date = [
		'id'=>$input['id'],
		];
		//更新的数据
		$update_map = [
			'account_id'=>$input['account_id'],
			'recharge_card'=>$input['recharge_card'],
			'recharge_time'=>$input['recharge_time'],
			'amount'=>$input['amount'],
			'status'=>$input['status'],
		];
		//获取数据
		$update_res = Db::name('recharge_card')->where($update_date)->update($update_map);
		if(!$update_res){
			$msg['msg'] = '更新据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $update_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 按条件删除记录
	* @description 按条件删除充值卡的数据
	* @author 开发者（范浩婷）
	* @url /rechargecard/recharge_card/delete
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:account_id type:VarChar require:1 default:0 other: desc:账户
	* @param name:recharge_card type:VarChar require:1 default:0 other: desc:充值卡
	* @param name:recharge_time type:Int require:1 default:0 other: desc:充值时间
	* @param name:amount type:Decimal require:1 default:0 other: desc:金额
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态
	*
	*
	* @return id:id
	* @return account_id:账户
	* @return recharge_card:充值卡
	* @return recharge_time:充值时间
	* @return amount:金额
	* @return status:状态
	*
	*/
	public function delete (){
		//模块 - 输入数据
		$input['id'] = Request::post('id/d');
		$input['account_id'] = Request::post('account_id');
		$input['recharge_card'] = Request::post('recharge_card');
		$input['recharge_time'] = Request::post('recharge_time/d');
		$input['amount'] = Request::post('amount');
		$input['status'] = Request::post('status');

		// ---  模块 - 删除  ---
		//删除的条件
		$temp_map = [
			['id','=',$input['id']],
			['account_id','=',$input['account_id']],
			['recharge_card','=',$input['recharge_card']],
			['recharge_time','=',$input['recharge_time']],
			['amount','=',$input['amount']],
			['status','=',$input['status']],
		];
		$delete_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2] == null || $v[2] == ''){
				continue;
			}
			array_push($delete_map,$v);
		}
		if(empty($delete_map)){
			$msg['msg'] = '删除条件不能为空';
			return $this->errorJson($msg);
		}

		//删除数据
		$delete_res = Db::name('recharge_card')->where($delete_map)->delete();
		if(!$delete_res){
			$msg['msg'] = '删除数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $delete_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
    /**
     * @title 用户充值
     * @description 用户充值
     * @author 开发者（范浩婷）
     * @url /rechargecard/recharge_card/recharge
     * @method post
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:Int require:1 default:0 other: desc:id
     * @param name:account_id type:VarChar require:1 default:0 other: desc:账户
     * @param name:recharge_card type:VarChar require:1 default:0 other: desc:充值卡
     * @param name:safety_code type:VarChar require:1 default:0 other: desc:安全码(卡密)
     *
     *
     *
     * @return id:id
     * @return account_id:账户
     * @return recharge_card:充值卡
     *
     */
    public function recharge()
    {

        $input['account_id']=Request::post('account_id');
        $input['recharge_card']=Request::post('recharge_card');
        $input['safety_code']=Request::post('safety_code');

        //用户充值到期时间增加

        $table=Config('database.zb_login');

        $original_data=Db::connect($table)->name('room_admin')->where('account_id',$input['account_id'])->field('expire_time,safety_code')->find();

        if($input['safety_code'] == $original_data['safety_code'])
        {
            $dates= date('Y-m-d H:i:s', strtotime("+1month",$original_data['expire_time']));

            $current_time = strtotime($dates);

            $res=Db::connect($table)->name('room_admin')->where('account_id',$input['account_id'])->update(array('expire_time'=>$current_time));

            if($res)
            {
                $msg['msg']="充值成功";

                return $this->successJson($msg);

            }else{

                $msg['msg']="充值失败";

                return $this->errorJson($msg);

            }
        }else{

            $msg['msg']="充值卡密错误";

            return $this->errorJson($msg);
        }
    }

}
